package com.pearl.jacksoncore.demo.feature;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

/**
 * @author TD
 * @version 1.0
 * @date 2024/4/2
 */
public class DeserializationFeatureTest {

    public static void main(String[] args) throws JsonProcessingException {
        JsonMapper jsonMapper = new JsonMapper();

        // FAIL_ON_UNKNOWN_PROPERTIES
        String jsonStr = "{\"id\":1767798780627279333,\"name\":\"张三\",\"pwd\":null,\"birthday\":1712043815828}";
        jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        User user = jsonMapper.readValue(jsonStr, User.class);

        // FAIL_ON_NULL_FOR_PRIMITIVES
        User userBy = new User();
        userBy.setName("仓大大");
        String value = jsonMapper.writeValueAsString(userBy);
        System.out.println(value);

        String str = "{\"id\":null,\"userId\":null}";
        jsonMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        User userByStr = jsonMapper.readValue(str, User.class);
        System.out.println(userByStr);

        //
/*        Color color = Color.RED;
        String strByColor = jsonMapper.writeValueAsString(color);
        System.out.println(strByColor);

        String strEnum="RED";
        Color colorByStr = jsonMapper.readValue(str, Color.class);
        System.out.println(colorByStr);*/

        // FAIL_ON_IGNORED_PROPERTIES
        String strByTest = "{\"id\":11,\"testAAA\":null}";
        jsonMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        User userByTest = jsonMapper.readValue(strByTest, User.class);
        System.out.println(userByTest);

        // UNWRAP_SINGLE_VALUE_ARRAYS
        String strByUNWRAP_SINGLE_VALUE_ARRAYS = "{\"name\": [\"John\"]}";
        jsonMapper.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true);
        User userByByUNWRAP_SINGLE_VALUE_ARRAYS = jsonMapper.readValue(strByUNWRAP_SINGLE_VALUE_ARRAYS, User.class);
        System.out.println(userByByUNWRAP_SINGLE_VALUE_ARRAYS);

        // ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
        String strByAAA ="{\"name\":\"\",\"age\":30,\"org\":\"\"}";
        jsonMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        User userByAAA  = jsonMapper.readValue(strByAAA, User.class);
        System.out.println(userByAAA);

        // ACCEPT_FLOAT_AS_INT
        String strByAAAA ="{\"name\":\"\",\"age\":30,\"age\":3.14159}";
        jsonMapper.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, true);
        User userByAAAA  = jsonMapper.readValue(strByAAAA, User.class);
        System.out.println(userByAAAA);

        // READ_ENUMS_USING_TO_STRING
        GenderEnum genderEnum = GenderEnum.MAN;
        jsonMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING,true);
        String strByGenderEnum = jsonMapper.writeValueAsString(genderEnum);
        System.out.println(GenderEnum.MAN.name());
        System.out.println(strByGenderEnum);

        jsonMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
        String strEnum="\"男\"";
        GenderEnum genderEnumByStr = jsonMapper.readValue(strEnum, GenderEnum.class);
        System.out.println(genderEnumByStr);

        // READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE
        String strEnumAAA="\"人妖\"";
        jsonMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
        GenderEnum strEnumAAAByStr = jsonMapper.readValue(strEnumAAA, GenderEnum.class);
        System.out.println(strEnumAAAByStr);
    }
}
